#include "newdialog.h"
#include <QLabel>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include "common.h"
#include <QFont>
#include <QPushButton>


NewSetting::NewSetting(QSize *size, QString title, QDialog *parent)
    : QDialog(parent)
    , m_title(title)
{
    setFixedWidth(Screen::GetScreenWidth()>>2);
    setFixedHeight(Screen::GetScreenHeight()>>2);

    setWindowTitle(m_title);

    btn_confirm = new QPushButton(QObject::tr("confirm"), this);
    btn_confirm->setFixedSize(100, 40);
    btn_confirm->setStyleSheet("QPushButton{background-color:;border:none;}"
                               "QPushButton:hover{background-color:border:none;}"
                               "QPushButton:pressed{background-color:;border:none;}");

    lab_width = new QLabel(QObject::tr("width:"), this);
    lab_height = new QLabel(QObject::tr("height:"), this);
    QFont f = lab_width->font();
    f.setPixelSize(30);
    lab_width->setFont(f);
    lab_height->setFont(f);
    line_width = new QLineEdit(this);
    line_height = new QLineEdit(this);
//    line_width->setStyleSheet("QLineEdit:{width:65px; height:50px;boeder:none; border-style:outset;}");
//    line_height->setStyleSheet("QLineEdit:{width:65px; height:50px;boeder:none; border-style:outset;}");
    line_width->setFixedSize(200, 40);
    line_height->setFixedSize(200, 40);

    lab_hint = new QLabel(QObject::tr("width or height is empty, please set again!"), this);
    lab_hint->setFixedSize(300, 20);
    lab_hint->setAlignment(Qt::AlignCenter);
    lab_hint->setGeometry(100, 160, 300, 20);
    lab_hint->hide();


    QHBoxLayout *layout_width = new QHBoxLayout;
    layout_width->addStretch();
    layout_width->addWidget(lab_width, 0, Qt::AlignCenter);
    layout_width->addWidget(line_width, 0, Qt::AlignCenter);
    layout_width->addStretch();

    QHBoxLayout *layout_height = new QHBoxLayout;
    layout_height->addStretch();
    layout_height->addWidget(lab_height, 0, Qt::AlignCenter);
    layout_height->addWidget(line_height, 0, Qt::AlignCenter);
    layout_height->addStretch();

    QHBoxLayout *layout_h = new QHBoxLayout;
    layout_h->addWidget(btn_confirm, 0, Qt::AlignCenter);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(layout_width);
    mainLayout->addLayout(layout_height);
    mainLayout->addLayout(layout_h);

    setLayout(mainLayout);

    connect(btn_confirm, &QPushButton::clicked, this, &NewSetting::checkConfirm);

    if (size)
    {
        m_size = size;
    }


}

void NewSetting::checkConfirm()
{
    if (line_width->text().isEmpty() || line_height->text().isEmpty())
    {
        lab_hint->show();
        return;
    }
    lab_hint->hide();
    if (m_size)
    {
        m_size->setWidth(line_width->text().toInt());
        m_size->setHeight(line_height->text().toInt());
    }

    accept();
}

